package com.lq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lq.mapper.UserMapper;
import com.lq.entity.User;

import java.util.Arrays;
import java.util.List;

/**
 * @author luoqian
 * @version 1.0
 * @date 2022-04-03 22:23
 */
@CrossOrigin
@RestController
@Api(tags = {"用户管理控制层"})
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "login")
    public List login(@RequestBody @ApiParam(name = "user对象", value = "user", required = true) User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", user.getAccount());
        queryWrapper.eq("password", user.getPassword());
        return userMapper.selectList(queryWrapper);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "register")
    public int register(@RequestBody @ApiParam(name = "user对象", value = "user", required = true) User user) {
        return userMapper.insert(user);
    }

    @PostMapping("/getUser/Page")
    @ApiOperation(value = "用户管理", notes = "getUser/Page")
    public Page userList(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize,@RequestBody @ApiParam(name = "user对象", value = "user", required = true) User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!("").equals(user.getUsername())){
            queryWrapper.like("username",user.getUsername());
        }
        if(!("").equals(user.getSex())){
            queryWrapper.eq("sex",user.getSex());
        }
        Page<User> userPage = new Page<>(pageNum, pageSize);
        return userMapper.selectPage(userPage, queryWrapper);
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "修改用户信息", notes = "/user/update")
    public int update(@RequestBody @ApiParam(name = "user", value = "user", required = true) User user){
        return userMapper.updateById(user);
    }

    @PostMapping("/user/delete")
    @ApiOperation(value = "批量删除用户信息", notes = "/user/delete")
    public int delete (@RequestParam @ApiParam(name = "ids",value = "1,2,3",required = true) String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return userMapper.deleteBatchIds(list);
    }

    @PostMapping("/user/byUsername")
    @ApiOperation(value = "获取当前用户用户信息", notes = "/user/byUsername")
    public List<User> getUserbyUsername(@RequestBody @ApiParam(name = "user", value = "user", required = true) User user){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username",user.getUsername());
            return userMapper.selectList(queryWrapper);
    }

    @GetMapping("/user/updatePassword")
    @ApiOperation(value = "修改密码", notes = "/user/updatePassword")
    public int updatePassword(@RequestParam @ApiParam(name = "password", value = "password", required = true) String password,@RequestParam @ApiParam(name = "id", value = "id", required = true) int id){
        User user = userMapper.selectById(id);
        user.setPassword(password);
        return userMapper.updateById(user);
    }
}
